
package com.borqs.music.util;

import static com.borqs.music.http.HttpConfiguration.APP_ID;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * Helper class to access BPC exported Api to other component.
 * 
 * @author b608
 */
public class BpcApiUtils {
    /**
     * the scheme, host of BPC, which are identical to those in
     * AndroidManifest.xml
     */
    private static final String SCHEME = "borqs";

    /**
     * the scheme path and search method to launch Stream Activity with specific
     * parameter, 3rd party could simply call wrapper method
     * startStreamActivityWithAppId or startStreamActivityWithStreamType rather
     * use such definitions directly.
     */
    public static final String SEARCH_KEY_UID = "uid"; // for user identity
    public static final String SEARCH_KEY_APPID = "appid";
    public static final String SEARCH_KEY_TYPE = "type";
    public static final String SEARCH_KEY_TAB = "tab";

    /**
     * Friends tab
     */
    public static int TAB_FRIENDS_CIRCLE = 0;
    public static int TAB_FRIENDS_PEOPLE = 1;
    public static int TAB_FRIENDS_SUGGESTION = 2; // People may know

    private static final String STREAM_PATH = "stream/details";
    private static final String STREAM_SCHEME_PREFIX = SCHEME + "://" + STREAM_PATH;

    private static final String FRIENDS_PATH = "friends/details";
    private static final String FRIENDS_SCHEME_PREFIX = SCHEME + "://" + FRIENDS_PATH;

    private static final String PROFILE_PATH = "profile/details";
    private static final String PROFILE_SCHEME_PREFIX = SCHEME + "://" + PROFILE_PATH;

    /**
     * Valid stream type, keep identical to server definition.
     */
    public static final int TEXT_POST = 1;
    public static final int IMAGE_POST = 1 << 1;
    public static final int VIDEO_POST = 1 << 2;
    public static final int AUDIO_POST = 1 << 3;
    public static final int BOOK_POST = 1 << 4;
    public static final int APK_POST = 1 << 5;
    public static final int LINK_POST = 1 << 6;
    public static final int APK_LINK_POST = 1 << 7;
    public static final int APK_COMMENT_POST = 1 << 8;
    public static final int APK_LIKE_POST = 1 << 9;
    public static final int BOOK_LIKE_POST = 1 << 10;
    public static final int BOOK_COMMENT_POST = 1 << 11;
    public static final int MAKE_FRIENDS_POST = 1 << 12;
    public static final int MUSIC_POST = 1 << 13;
    public static final int MUSIC_COMMENT_POST = 1 << 14;
    public static final int MUSIC_LIKE_POST = 1 << 15;

    /**
     * Helper shortcut definition for ALL posts.
     */
    public static final int ALL_TYPE_POSTS = -1;

    /**
     * Helper shortcut definition for Apk posts.
     */
    public static final int ONLY_APK_POST = (APK_POST | APK_LIKE_POST | APK_COMMENT_POST | APK_LINK_POST);
    public static final int ALL_APK_POST = (TEXT_POST | ONLY_APK_POST);

    /**
     * Helper shortcut definition for Book posts.
     */
    public static final int ONLY_BOOK_POST = (BOOK_POST | BOOK_LIKE_POST | BOOK_COMMENT_POST);
    public static final int ALL_BOOK_POST = (TEXT_POST | ONLY_BOOK_POST);

    public static final int ONLY_MUSIC_POST = (MUSIC_POST | MUSIC_LIKE_POST | MUSIC_COMMENT_POST);
    public static final int ALL_MUSIC_POST = (TEXT_POST | ONLY_MUSIC_POST);

    /**
     * Launch Stream Activity for specific applications. The alternative method
     * is startStreamActivityWithStreamType
     * 
     * @param context
     * @param appId
     */
    public static boolean startFriendsActivity(Context context, String userId) {
        return startFriendsActivityWithTab(context, userId, TAB_FRIENDS_CIRCLE);
    }

    public static boolean startFriendsActivityWithTab(Context context, String userId, int tab) {
        final String schemeTxt = FRIENDS_SCHEME_PREFIX +
                "?" + SEARCH_KEY_APPID + "=" + APP_ID +
                "&" + SEARCH_KEY_UID + "=" + userId +
                "&" + SEARCH_KEY_TAB + "=" + tab;

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(schemeTxt));
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return startActivity(context, intent);
    }

    /**
     * Launch Stream Activity for specific applications. The alternative method
     * is startStreamActivityWithStreamType
     * 
     * @param context
     * @param appId
     */
    public static boolean startProfileActivity(Context context, String userId) {
        final String schemeTxt = PROFILE_SCHEME_PREFIX +
                "?" + SEARCH_KEY_APPID + "=" + APP_ID +
                "&" + SEARCH_KEY_UID + "=" + userId;
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(schemeTxt));
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return startActivity(context, intent);
    }

    /**
     * Launch Stream Activity for specific Stream type. The alternative method
     * is startStreamActivityWithAppId.
     * 
     * @param context
     * @param type
     */
    public static boolean startStreamActivity(Context context) {
        int type = ONLY_MUSIC_POST;
        final String schemeTxt = STREAM_SCHEME_PREFIX + "?" + SEARCH_KEY_TYPE + "=" + type;
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(schemeTxt));
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        return startActivity(context, intent);
    }

    public static boolean isActivityReadyForIntent(Context context, Intent intent) {
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
        return activities.size() > 0;
    }

    private static boolean startActivity(Context context, Intent intent) {
        if (isActivityReadyForIntent(context, intent)) {
            context.startActivity(intent);
            return true;
        } else {
            return false;
        }
    }
    
    public static interface Action {
        public final static String SHARE_SOURCE = "com.borqs.bpc.action.SHARE_SOURCE";
        public final static String SHARE_SOURCE_RESULT = "com.borqs.bpc.action.SHARE_SOURCE_RESULT";
    }

    public static interface Result {
        public final static String PACKAGE_NAME = "package";
        public final static String SOURCE_LABEL = "label";
        public final static String SOURCE_COUNT = "count";
        public final static String CALLBACK_SCHEME = "scheme";
    }

    public static interface User {
        public static final String USER_ID           =  "USER_ID";
        public static final String USER_NAME         =  "USER_NAME";
        public static final String USER_CIRCLE       =  "USER_CIRCLE";
    }
    
    public static String parseSchemeValue(final Intent intent, final String key) {
        if (null != intent) {
            final Uri uri = intent.getData();
            if (null != uri) {
                return uri.getQueryParameter(key);
            }
        }

        return null;
    }
    
    public static final String KEY_FOR_MUSIC_SERVER = "music_host";
    public static final String KEY_FOR_API_SERVER = "api_host";

    public static String getServerURL(Context context, String appKey) {
        String urlHost = "";
        final Uri SETTINGS_CONTENT_URI = Uri.parse("content://com.borqs.qiupu/settings");
        final String[] projects = new String[] {
                "_id", "name", "value",
        };
        final String where = "name='" + appKey + "'";
        Cursor cursor = context.getContentResolver().query(SETTINGS_CONTENT_URI, projects, where,
                null, null);
        if (null == cursor || cursor.getCount() < 1) {
            Log.w("BpcApiUtils", "getCurrentServerUrl, fail to query cursor.");
        } else {
            cursor.moveToFirst();
            urlHost = cursor.getString(cursor.getColumnIndex("value"));
            cursor.close();
        }
        return urlHost;

    }
}
